<template>
  <el-card>
    <el-table border row-key="id" :data="permissionList">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作">
        <template #="{ row }">
          <el-button type="primary" icon="Plus"
                     :disabled="row.level === 4"
                     @click="handleAddPermission(row)">
            {{ row.level === 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button type="warning" icon="Edit" :disabled="row.level === 1"
                     @click="handleUpdatePermission(row)">编辑</el-button>
          <el-popconfirm :title="`确定删除${row.name}吗？`"
                         @confirm="deletePermissionById(row.id)"
                         width="200px">
            <template #reference>
              <el-button type="danger" icon="Delete" :disabled="row.level === 1">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="isShowAddOrUpdatePermissionDialog" :title="menuData.id ? '修改菜单' : '添加菜单'">
      <el-form label-width="80px">
        <el-form-item label="菜单名称">
          <el-input placeholder="请输入菜单名称" v-model="menuData.name"></el-input>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input placeholder="请输入权限值" v-model="menuData.code"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShowAddOrUpdatePermissionDialog = false">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { reqAddOrUpdatePermission, reqAllPermission, reqDeletePermissionById } from '@/api/acl/permission';
import type { PermissionList, PermissionData, AllPermissionResponseData, MenuParams } from '@/api/acl/permission/type';
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';

const permissionList = ref<PermissionList>([])

const isShowAddOrUpdatePermissionDialog = ref<boolean>(false)

onMounted(() => {
  getAllMenu()
})

// 获取菜单
const getAllMenu = async () => {
  const result: AllPermissionResponseData = await reqAllPermission()
  if (result.code === 200) {
    permissionList.value = result.data
  }
}

// 菜单接口参数
const menuData = ref<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})

// 添加菜单按钮的回调
const handleAddPermission = (row: PermissionData) => {
  // 切换对话框显示
  isShowAddOrUpdatePermissionDialog.value = true
  Object.assign(menuData.value, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  // 收集数据 level 与  pid
  menuData.value.level = row.level + 1
  menuData.value.pid = row.id as number
}

// 编辑菜单按钮的回调
const handleUpdatePermission = (row: PermissionData) => {
  isShowAddOrUpdatePermissionDialog.value = true
  Object.assign(menuData.value, row)
}

// 确定按钮的回调
const confirm = async () => {
  const result: any = await reqAddOrUpdatePermission(menuData.value)
  if (result.code === 200) {
    ElMessage.success(menuData.value.id ? '修改成功' : '添加成功')
    isShowAddOrUpdatePermissionDialog.value = false
  } else {
    ElMessage.error(result.data)
    isShowAddOrUpdatePermissionDialog.value = false
  }
}

// 删除菜单
const deletePermissionById = async (id: number) => {
  const result: any = await reqDeletePermissionById(id)
  if (result.code === 200) {
    ElMessage.success('删除成功')
    getAllMenu()
  } else {
    ElMessage.error(result.data)
  }
}
</script>

<style scoped></style>
